//
//  MORegistViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/7/29.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MORegistViewController.h"
#import "MOForgotPasswordViewCell.h"
#import "MORegistSureViewController.h"

@interface MORegistViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIButton *backBtn;

@property (nonatomic, strong) UITableView *tableView;
//数据数组
@property(nonatomic,strong)NSMutableArray *dataArray;
//footer
@property(nonatomic,strong)UIView *footerView;
//确认按钮
@property(nonatomic,strong)UIButton *sureSetButton;
//取消按钮
@property(nonatomic,strong)UIButton *cancelSetButton;


@end

@implementation MORegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.backBtn];
    
    [self creatView];
    
    [self initData];
}


-(void)creatView
{
    [self.view addSubview:self.tableView];
    
    self.tableView.tableFooterView = self.footerView;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(clickBackBtn) name:@"registSucess" object:nil];
}


-(void)initData
{
    self.dataArray = [NSMutableArray array];
    NSArray *nameArr = @[@"姓名",@"手机号"];
    NSArray *paleArr = @[@"请输入姓名",@"请输入手机号"];
    for (int i = 0; i<nameArr.count; i++) {
        MOForgotPasswordModel *model = [[MOForgotPasswordModel alloc]init];
        model.nameStr = nameArr[i];
        model.placeStr = paleArr[i];
        model.type = @"0";
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MOForgotPasswordViewCell *setCell = [MOForgotPasswordViewCell cellWithTableView:tableView andIndexPath:indexPath];
    setCell.model = self.dataArray[indexPath.row];
    return setCell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(12)*4+12+15+13;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        
    }
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 35, 0, 35)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 35, 0, 35)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 35, 0, 35)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 35, 0, 35)];
    }
}

#pragma mark -- 返回
-(void)clickBackBtn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark -- 确定
-(void)clickSureResetButton
{

    MOForgotPasswordViewCell *cell0 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    MOForgotPasswordViewCell *cell1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

    if (cell0.textField.text.length==0) {
        for (int i = 0; i<self.dataArray.count; i++) {
            MOForgotPasswordModel *model = self.dataArray[i];
            if (i==0) {
                model.showField = @"1";
                model.fieldNameStr = @"姓名不能为空";
            }else{
                model.showField = @"0";
                model.fieldNameStr = @"";
            }
        }
        [self.tableView reloadData];
        return;
    }
    
    if (cell1.textField.text.length!=11) {
        for (int i = 0; i<self.dataArray.count; i++) {
            MOForgotPasswordModel *model = self.dataArray[i];
            if (i==1) {
                model.showField = @"1";
                model.fieldNameStr = @"手机号有误，请重填";
            }else{
                model.showField = @"0";
                model.fieldNameStr = @"";
            }
        }
        [self.tableView reloadData];
        return;
    }
    
    for (int i = 0; i<self.dataArray.count; i++) {
        MOForgotPasswordModel *model = self.dataArray[i];
        model.showField = @"0";
        model.fieldNameStr = @"";
    }
    [self.tableView reloadData];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *postUrl = [NSString stringWithFormat:@"%@",ADDRESSBOOK_ISABEXIST_URL];
    NSDictionary *parameters = @{
                                 @"mobile":cell0.textField.text,
                                 @"truename":cell1.textField.text
                                 };
    
    [PPNetworkHelper GET:postUrl parameters:parameters success:^(id responseObject) {
        NSLog(@"验证通讯录中是否有该手机号:%@",responseObject);
        
        if ([responseObject integerValue]==1) {//如果true，表示通讯录存在该用户，接着验证后台是否该手机号注册
            
            [self requestForUserAndTel:cell1.textField.text name:cell0.textField.text];
            
        }else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [[ADShowMessage shareShowMessage]showTextOnly:@"您暂没有权限注册，请联系管理员！" showInView:self.view];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    

}

-(void)requestForUserAndTel:(NSString *)tel name:(NSString *)name
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?phone=%@",USER_ISEXIST_URL,tel];
    NSDictionary *parameters = @{
                                 };
    [PPNetworkHelper GET:postUrl parameters:parameters success:^(id responseObject) {
        NSLog(@"验证后台该手机号是否已经注册:%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject integerValue]==1) {
            
            [[ADShowMessage shareShowMessage]showTextOnly:@"该手机号已经注册！" showInView:self.view];
            
        }else{//如果为false表示该手机号尚未在后台注册，跳转到注册详情页面（同时将该手机号与用户姓名传入）
            MORegistSureViewController *vc = [[MORegistSureViewController alloc]init];
            vc.telStr = tel;
            vc.nameStr = name;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- 懒加载
- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 30)];
        [_backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
        [_backBtn setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [_backBtn setImage:IMAGE(@"nav_back") forState:UIControlStateNormal];
        [_backBtn setTitleColor:KTabbarBlueColor forState:UIControlStateNormal];
        _backBtn.titleLabel.font = FONT_SIZE(15);
    }
    return _backBtn;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight-TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
        
        _sureSetButton = [[UIButton alloc]initWithFrame:CGRectMake(35, 10, SCREEN_WIDTH-70, 45)];
        [_sureSetButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureSetButton addTarget:self action:@selector(clickSureResetButton) forControlEvents:UIControlEventTouchUpInside];
        [_sureSetButton setBackgroundColor:KBlueColor_2C94FF];
        _sureSetButton.titleLabel.font = FONT_SIZE(15);
        _sureSetButton.layer.masksToBounds = YES;
        _sureSetButton.layer.cornerRadius = 3.0;
        [_footerView addSubview:_sureSetButton];
        
        _cancelSetButton = [[UIButton alloc]initWithFrame:CGRectMake(35, _sureSetButton.bottom + 33, SCREEN_WIDTH-70, 45)];
        [_cancelSetButton setTitle:@"返回" forState:UIControlStateNormal];
        [_cancelSetButton addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
        [_cancelSetButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        _cancelSetButton.titleLabel.font = FONT_SIZE(15);
        _cancelSetButton.layer.masksToBounds = YES;
        _cancelSetButton.layer.cornerRadius = 3.0;
        _cancelSetButton.layer.borderWidth = 1;
        _cancelSetButton.layer.borderColor = KGrayColor_d7d7d7.CGColor;
        [_footerView addSubview:_cancelSetButton];
        
        _footerView.height = _cancelSetButton.bottom + 20;
    }
    return _footerView;
}

@end
